package com.kailong.servlet.seat;

import com.kailong.dao.admin.ManageSeatDao;
import com.kailong.pojo.Seat;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static java.lang.System.out;

@WebServlet("/admin/seatAdd")
public class SeatAdd extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Integer tno = null;
        //获取选择的楼层
        String tfloor = request.getParameter("floor");
        //获取选择的区域
        String tsquare = request.getParameter("square");
        //获取状态
        String tstate = request.getParameter("state");
        //获取编号
        String no = request.getParameter("tno");
        //获取备注
        String tinfo = request.getParameter("tinfo");
        try{
            tno = Integer.parseInt(no);
        } catch (NumberFormatException e) {
            PrintWriter out = response.getWriter();
            out.print("<script>");
            out.print("alert('请输入合法数字！');");
            out.print("location.href='adminSeatAdd.jsp'");
            out.print("</script>");
        }
        Seat s = new Seat(tno,tfloor,tsquare,tstate,tinfo);
        int count = ManageSeatDao.insert(s);
        if(count > 0){
            PrintWriter out = response.getWriter();
            out.print("<script>");
            out.print("alert('添加成功');");
            out.print("location.href='adminSeatAdd.jsp'");
            out.print("</script>");
        }else{
            PrintWriter out = response.getWriter();
            out.print("<script>");
            out.print("alert('添加失败');");
            out.print("location.href='adminSeatAdd.jsp'");
            out.print("</script>");

        }
    }
}
